package cn.imvc.app.controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;

import cn.imvc.app.entities.GoodsPrice;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.GoodsPriceModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.util.Wechat;

@Controller
public class GoodsSkuControllerImpl implements GoodsSkuController {
	@Autowired
	private GoodsSkuModel goodsSkuModel;
	@Autowired
	private GoodsPriceModel goodsPriceModel;
	@Autowired
	private MemberModel memberModel;
	@Autowired
	Wechat wechat;
	
	@RequestMapping("{skuId}.html")
	public String showDetail(@PathVariable Integer skuId, Model model) {
		//手机端
		int platform =1;
		Map<String, Object> result = goodsSkuModel.showDetail(skuId,platform);
		if(result==null||result.size()==0){
			return "redirect:/index.html";
		}
		model.addAllAttributes(result);
		return "goods/detail";
	}

	@RequestMapping("/goodsSku/getInventoryAndIncreaseClick.json")
	public @ResponseBody Integer getInventory(Integer goodsId, Integer skuId) {
		if(goodsId==null||goodsId==0){
			return 0;
		}
		return goodsSkuModel.saveIncreaseClickAndGetInventory(goodsId, skuId);
	}

	@RequestMapping("/goodsSku/getSkuPrices.html")
	public String getSkuPrices(Integer skuId, String token,Model model) {
		Member member = memberModel.getLoginMember(token);
		Map<String,Object> skuPrices = goodsPriceModel.getGoodsPricesByMemberIdAndGoodsSkuId(skuId, member);
		model.addAllAttributes(skuPrices);
		return "goods/sku_prices";
	}
	
	@RequestMapping("/getWxconfig.json")
	public @ResponseBody Map<String,Object> getWxconfig(String signatureUrl) {
		String appId = wechat.getAppId();
		JsAPI jsapi = new JsAPI(wechat.getJsApiConfig());
		GetSignatureResponse jsconfig =null;
		try {
			String decoderURL = URLDecoder.decode(signatureUrl,"utf-8");
			jsconfig = jsapi.getSignature(decoderURL);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("appId", appId);
		result.put("jsconfig", jsconfig);
		result.put("signatureUrl", signatureUrl);
		return result;
	}
	//--------pc------------
	@RequestMapping("{skuId}.htm")
	public String pcShowDetail(@PathVariable Integer skuId, Model model) {
		//pc端
		int platform =2;
		Map<String, Object> result = goodsSkuModel.showDetail(skuId,platform);
		if(result==null||result.size()==0){
			return "redirect:/index.htm";
		}
		model.addAllAttributes(result);
		return "goods/pcdetail";
	}
	@RequestMapping("/goodsSku/getSkuPrices.htm")
	public String pcGetSkuPrices(Integer skuId, String token,Model model) {
		Member member = memberModel.getLoginMember(token);
		Map<String,Object> skuPrices = goodsPriceModel.getGoodsPricesByMemberIdAndGoodsSkuId(skuId, member);
		model.addAllAttributes(skuPrices);
		return "goods/pc_sku_prices";
	}
}
